package com.xm.controller;

import com.ramostear.captcha.HappyCaptcha;
import com.ramostear.captcha.support.CaptchaStyle;
import com.xm.mapper.AdminMapper;
import com.xm.pojo.Admin;
import com.xm.pojo.User;
import com.xm.service.AdminService;
import com.xm.until.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("captcha")
    public void happyCaptcha(HttpServletRequest request, HttpServletResponse response) {
        HappyCaptcha.require(request, response)
                .style(CaptchaStyle.ANIM)
                .length(4)
                .height(48)
                .width(130)
                //.font(Fonts.getInstance().zhFont())
                .build().finish();
    }

    @RequestMapping("login")
    public Map verify(String code, String username, String password, HttpServletRequest request, HttpSession session) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        boolean flag = HappyCaptcha.verification(request, code, true);
        if (flag) {
            Admin admin = adminService.findByUsername(username);
            if (admin == null) {
                map.put("code",0);
                map.put("msg","null");
            } else if (password.equals(admin.getPassword())) {
                //response.sendRedirect(request.getContextPath() + "/index.html");
                HappyCaptcha.remove(request);
                session.setAttribute("admin", admin);
                //new Cookie();
                map.put("code",200);
                map.put("msg","success");
                map.put("data",admin);
            } else {
                map.put("code",1);
                map.put("msg","fail");
            }
        } else {
            map.put("code",2);
            map.put("msg","code");
        }return map;
    }

    @RequestMapping("logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //需要删除session中存储的admin
        //request.getSession().removeAttribute("admin");//移除session中的属性
        request.getSession().invalidate();//销毁session

        response.sendRedirect(request.getContextPath()+"/login.html");
    }

    @RequestMapping("edit")
    public String updateAdmin(Admin admin) {
        adminService.updateAdmin(admin);
        return "ok";
    }

    @RequestMapping("upload image")
    @ResponseBody
    public Map<String,Object> uploadimage(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String imagePath = UploadUtil.upload(file);//UploadUtil是一个工具类，返回的是图片的存储路径
        System.out.println(imagePath);
        if(imagePath!=null){
            map.put("code", 1);
            map.put("msg", "上传成功");
            map.put("src", imagePath);
        }else{
            map.put("code", 0);
            map.put("msg", "上传失败");
        }
        return map;
    }

    @RequestMapping("add")//添加接口
    public void add(Admin admin, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println(admin+"--------------------------");
        adminService.add(admin);
        //添加完之后，跳转到首页
        response.sendRedirect(request.getContextPath() + "/adminAdd.html");
    }
}
